package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.pro.competitor.pojo.dto.CompetitorAnalysisDeleteDTO;
import com.xbongbong.pro.competitor.pojo.dto.CompetitorAnalysisFormGetDTO;
import com.xbongbong.pro.competitor.pojo.dto.CompetitorAnalysisFormSaveDTO;
import com.xbongbong.pro.competitor.pojo.dto.CompetitorAnalysisListDTO;
import com.xbongbong.pro.competitor.pojo.dto.CompetitorAnalysisSearchListDTO;
import com.xbongbong.pro.competitor.pojo.dto.CompetitorAnalysisSwotGetDTO;
import com.xbongbong.pro.competitor.pojo.dto.CompetitorAnalysisSwotUpdateDTO;
import com.xbongbong.pro.competitor.pojo.dto.CompetitorAnalysisUpdateDTO;
import com.xbongbong.pro.competitor.pojo.dto.CompetitorAnalysisUpdateGetDTO;
import com.xbongbong.pro.competitor.pojo.vo.CompetitorAnalysisDeleteVO;
import com.xbongbong.pro.competitor.pojo.vo.CompetitorAnalysisFormGetVO;
import com.xbongbong.pro.competitor.pojo.vo.CompetitorAnalysisFormSaveVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.competitor.pojo.vo.CompetitorAnalysisListVO;
import com.xbongbong.pro.competitor.pojo.vo.CompetitorAnalysisSearchListVO;
import com.xbongbong.pro.competitor.pojo.vo.CompetitorAnalysisSwotGetVO;
import com.xbongbong.pro.competitor.pojo.vo.CompetitorAnalysisSwotUpdateVO;
import com.xbongbong.pro.competitor.pojo.vo.CompetitorAnalysisUpdateGetVO;
import com.xbongbong.pro.competitor.pojo.vo.CompetitorAnalysisUpdateVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.service.CompetitorAnalysisService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author: yingjf
 * @date: 2022/8/17 10:59
 * @description:竞争对手分析相关接口
 */
@SuppressWarnings("deprecation")
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/competitorAnalysis")
public class CompetitorAnalysisController {
    @Resource
    private CompetitorAnalysisService competitorAnalysisService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
     * 竞争对手分析表单解释获取
     * date: 2022/8/17 11:12 上午
     * @author yingjf
     * @param competitorAnalysisFormGetDTO:
     * @param br:
     */
    @RequestMapping(value = "/form/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String formGet(@RequestBody @Valid CompetitorAnalysisFormGetDTO competitorAnalysisFormGetDTO, BindingResult br){
        XbbResponse<CompetitorAnalysisFormGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CompetitorAnalysisFormGetVO competitorAnalysisFormGetVO = competitorAnalysisService.formGet(competitorAnalysisFormGetDTO);
                response = new XbbResponse<>(competitorAnalysisFormGetVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 竞争对手分析表单解释保存
     * date: 2022/8/17 2:10 下午
     * @author yingjf
     * @param competitorAnalysisFormSaveDTO:
     * @param br:
     */
    @RequestMapping(value = "/form/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String formSave(@RequestBody @Valid CompetitorAnalysisFormSaveDTO competitorAnalysisFormSaveDTO, BindingResult br) throws XbbException {
        XbbResponse<CompetitorAnalysisFormSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(competitorAnalysisFormSaveDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(competitorAnalysisFormSaveDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {
                CompetitorAnalysisFormSaveVO competitorAnalysisFormSaveVO = competitorAnalysisService.formSave(competitorAnalysisFormSaveDTO);
                response = new XbbResponse<>(competitorAnalysisFormSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 机会详情tab查询竞争对手列表
     * date: 2022/8/25 7:29 下午
     * @author yingjf
     * @param competitorAnalysisListDTO:
     * @param br:
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid CompetitorAnalysisListDTO competitorAnalysisListDTO, BindingResult br) throws XbbException {
        XbbResponse<CompetitorAnalysisListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CompetitorAnalysisListVO competitorAnalysisListVO = competitorAnalysisService.list(competitorAnalysisListDTO);
                response = new XbbResponse<>(competitorAnalysisListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 机会详情tab竞争对手分析编辑数据获取
     * date: 2022/8/26 3:09 下午
     * @author yingjf
     * @param competitorAnalysisUpdateGetDTO:
     * @param br:
     */
    @RequestMapping(value = "/update/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateGet(@RequestBody @Valid CompetitorAnalysisUpdateGetDTO competitorAnalysisUpdateGetDTO, BindingResult br) throws XbbException {
        XbbResponse<CompetitorAnalysisUpdateGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CompetitorAnalysisUpdateGetVO competitorAnalysisUpdateGetVO = competitorAnalysisService.updateGet(competitorAnalysisUpdateGetDTO);
                response = new XbbResponse<>(competitorAnalysisUpdateGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 机会详情tab竞争对手分析删除
     * date: 2022/8/26 3:58 下午
     * @author yingjf
     * @param competitorAnalysisDeleteDTO:
     * @param br:
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid CompetitorAnalysisDeleteDTO competitorAnalysisDeleteDTO, BindingResult br) throws XbbException {
        XbbResponse<CompetitorAnalysisDeleteVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CompetitorAnalysisDeleteVO competitorAnalysisDeleteVO = competitorAnalysisService.delete(competitorAnalysisDeleteDTO);
                response = new XbbResponse<>(competitorAnalysisDeleteVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 机会详情tab竞争对手分析编辑数据
     * date: 2022/8/26 3:09 下午
     * @author yingjf
     * @param competitorAnalysisUpdateDTO:
     * @param br:
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid CompetitorAnalysisUpdateDTO competitorAnalysisUpdateDTO, BindingResult br) throws XbbException {
        XbbResponse<CompetitorAnalysisUpdateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CompetitorAnalysisUpdateVO competitorAnalysisUpdateVO = competitorAnalysisService.update(competitorAnalysisUpdateDTO);
                response = new XbbResponse<>(competitorAnalysisUpdateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 竞争对手分析swot获取(机会详情tab竞争对手分析处调用)
     * date: 2022/8/29 3:51 下午
     * @author yingjf
     * @param competitorAnalysisSwotGetDTO:
     * @param br:
     */
    @RequestMapping(value = "/swot/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String swotGet(@RequestBody @Valid CompetitorAnalysisSwotGetDTO competitorAnalysisSwotGetDTO, BindingResult br) throws XbbException {
        XbbResponse<CompetitorAnalysisSwotGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CompetitorAnalysisSwotGetVO competitorAnalysisSwotGetVO = competitorAnalysisService.swotGet(competitorAnalysisSwotGetDTO);
                response = new XbbResponse<>(competitorAnalysisSwotGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 竞争对手分析swot更新
     * date: 2022/8/29 4:13 下午
     * @author yingjf
     * @param competitorAnalysisSwotUpdateDTO:
     * @param br:
     */
    @RequestMapping(value = "/swot/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String swotUpdate(@RequestBody @Valid CompetitorAnalysisSwotUpdateDTO competitorAnalysisSwotUpdateDTO, BindingResult br) throws XbbException {
        XbbResponse<CompetitorAnalysisSwotUpdateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CompetitorAnalysisSwotUpdateVO competitorAnalysisSwotUpdateVO = competitorAnalysisService.swotUpdate(competitorAnalysisSwotUpdateDTO);
                response = new XbbResponse<>(competitorAnalysisSwotUpdateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 竞争对手分析swot左上角的搜索接口
     * date: 2022/8/29 4:13 下午
     * @author yingjf
     * @param competitorAnalysisSearchListDTO:
     * @param br:
     */
    @RequestMapping(value = "/swot/searchList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String searchList(@RequestBody @Valid CompetitorAnalysisSearchListDTO competitorAnalysisSearchListDTO, BindingResult br) throws XbbException {
        XbbResponse<CompetitorAnalysisSearchListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CompetitorAnalysisSearchListVO competitorAnalysisSearchListVO = competitorAnalysisService.searchList(competitorAnalysisSearchListDTO);
                response = new XbbResponse<>(competitorAnalysisSearchListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
